package com.infore.statisticsAndAlarm.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;








public class GetAQI {

	
//	public static void main(String[] args){
//  	 Map<String, Double> map = new HashMap<String, Double>();
////		Map<String, Double> map1 = new HashMap<String, Double>();
////		Map<String, Double> map2 = new HashMap<String, Double>();
////		Map<String, Double> map3 = new HashMap<String, Double>();
////		Map<String, Double> map4 = new HashMap<String, Double>();
////		Map<String, Double> map5 = new HashMap<String, Double>();
////		Map<String, Double> map6 = new HashMap<String, Double>();
//		map.put("SO2",39.44);
//		map.put("NO2", 0.0);
//		map.put("PM10", 0.0);
//		map.put("CO", 0.0);
//		map.put("O3", 0.0);
//		map.put("PM25", 115.52);
////		map.put("O38", 99.0);
//		System.out.println(map);
//		System.out.println("map的AQI是："+getAQIMap(map));
//		System.out.println("map的首要污染物是："+getPrimaryPollution(map));
////		System.out.println("map1的AQI是："+GetAQI.getAQIMap(map1));
////		System.out.println("map2的AQI是："+GetAQI.getAQIMap(map2));
////		System.out.println("map3的AQI是："+GetAQI.getAQIMap(map3));
////		System.out.println("map4的AQI是："+GetAQI.getAQIMap(map4));
////		System.out.println("map5的AQI是："+GetAQI.getAQIMap(map5));
////		System.out.println("map6的AQI是："+GetAQI.getAQIMap(map6));
//	}
	
	
	/**
	 * 
	 *  
	 * 
	 * @author cys
	 * @date 2016年12月23日
	 * @param map key 因子名称 value 因子值  1小时
	 * @return 多因子的微指数
	 */
	public static int getAQIMap(Map<String,Double> map){
		int IAQI_SO2_1H =0;
		int IAQI_NO2_1H =0;
		int IAQI_PM10_1H =0;
		int IAQI_CO_1H =0;
		int IAQI_O3_1H =0;
		int IAQI_PM25_1H =0;
		int IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			 IAQI_SO2_1H = GetAQI.getIAQI("SO2",map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_1H = GetAQI.getIAQI("NO2",map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = GetAQI.getIAQI("PM10",map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_1H = GetAQI.getIAQI("CO",map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = GetAQI.getIAQI("O3",map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = GetAQI.getIAQI("PM2.5",map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = GetAQI.getIAQI("O38H", map.get("O38H"));
		}
		if(map.get("PM2.5") !=null){
			IAQI_PM25_1H = GetAQI.getIAQI("PM2.5",map.get("PM2.5"));
		}
		if(map.get("SO2") == null && map.get("NO2") == null && map.get("PM10") == null && map.get("CO") == null && 
				map.get("O3") == null && map.get("PM25") == null && map.get("O38H") == null && map.get("PM2.5") == null){
			return -1;
		}else{
			int IAQI = GetAQI.Max(IAQI_SO2_1H, IAQI_NO2_1H, IAQI_PM10_1H, IAQI_CO_1H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
			return IAQI;
		}
		
	}
	
	/**
	 * 微指数24小时
	 * @param map
	 * @return
	 */
	public static int getAQI24HMap(Map<String,Double> map){
		int IAQI_SO2_24H =0;
		int IAQI_NO2_24H =0;
		int IAQI_PM10_1H =0;
		int IAQI_CO_24H =0;
		int IAQI_O3_1H =0;
		int IAQI_PM25_1H =0;
		int IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			IAQI_SO2_24H = GetAQI.getIAQI_24H("SO2",map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_24H = GetAQI.getIAQI_24H("NO2",map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = GetAQI.getIAQI_24H("PM10",map.get("PM10"));
		}
		if(map.get("CO") !=null){
			IAQI_CO_24H = GetAQI.getIAQI_24H("CO",map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = GetAQI.getIAQI_24H("O3",map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = GetAQI.getIAQI_24H("PM2.5",map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = GetAQI.getIAQI_24H("O38H", map.get("O38H"));
		}
			
		int IAQI = GetAQI.Max(IAQI_SO2_24H, IAQI_NO2_24H, IAQI_PM10_1H, IAQI_CO_24H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		return IAQI;
	}
	
	//获取空气质量指数级别
	public static String getPollutionLevel(int AQI){
		String airquality = null;
		if(AQI >=0 && AQI<= 50){
			airquality = "优";
		}else if(AQI >=51 && AQI<= 100){
			airquality = "良";
		}else if(AQI >=101 && AQI<= 150){
			airquality = "轻度污染";
		}else if(AQI >=151 && AQI<= 200){
			airquality = "中度污染";
		}else if(AQI >=201 && AQI<= 300){
			airquality = "重度污染";
		}else if(AQI > 300){
			airquality = "严重污染";
		}
		return airquality;
	}
	
	//获取空气质量指数类别颜色
	public static String getPollutionColor(int AQI){
		String airColor = null;
		if(AQI >=0 && AQI<= 50){
			airColor = "绿色";
		}else if(AQI >=51 && AQI<= 100){
			airColor = "黄色";
		}else if(AQI >=101 && AQI<= 150){
			airColor = "橙色";
		}else if(AQI >=151 && AQI<= 200){
			airColor = "红色";
		}else if(AQI >=201 && AQI<= 300){
			airColor = "紫色";
		}else if(AQI > 300){
			airColor = "褐红色";
		}
		return airColor;
	}
	//获取空气质量指数类别颜色
	public static String getPollutionColorRGB(int AQI){
		String airColor = null;
		if(AQI >=0 && AQI<= 50){
			airColor = "#00e400";
		}else if(AQI >=51 && AQI<= 100){
			airColor = "#ffff00";
		}else if(AQI >=101 && AQI<= 150){
			airColor = "#ff7e00";
		}else if(AQI >=151 && AQI<= 200){
			airColor = "#ff0000";
		}else if(AQI >=201 && AQI<= 300){
			airColor = "#99004c";
		}else if(AQI > 300){
			airColor = "#7e0023";
		}else{
			airColor = "#7e0023";
		}
		return airColor;
	}
	//获取空气质量指数类别
	public static String getPollutionNumLevel(int AQI){
		String airNumLevel = null;
		if(AQI >=0 && AQI<= 50){
			airNumLevel = "一级";
		}else if(AQI >=51 && AQI<= 100){
			airNumLevel = "二级";
		}else if(AQI >=101 && AQI<= 150){
			airNumLevel = "三级";
		}else if(AQI >=151 && AQI<= 200){
			airNumLevel = "四级";
		}else if(AQI >=201 && AQI<= 300){
			airNumLevel = "五级";
		}else if(AQI > 300){
			airNumLevel = "六级";
		}
		return airNumLevel;
	}
	
	//获取首要污染物（1小时）
	public static String getPrimaryPollution(Map<String, Double> map){

		int IAQI_SO2_1H =0;
		int IAQI_NO2_1H =0;
		int IAQI_PM10_1H =0;
		int IAQI_CO_1H =0;
		int IAQI_O3_1H =0;
		int IAQI_PM25_1H =0;
		int IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			 IAQI_SO2_1H = GetAQI.getIAQI("SO2",map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_1H = GetAQI.getIAQI("NO2",map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = GetAQI.getIAQI("PM10",map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_1H = GetAQI.getIAQI("CO",map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = GetAQI.getIAQI("O3",map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = GetAQI.getIAQI("PM2.5",map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = GetAQI.getIAQI("O38H", map.get("O38H"));
		}
		StringBuffer sBuffer = new StringBuffer("");
		int AQI = GetAQI.Max(IAQI_SO2_1H, IAQI_NO2_1H, IAQI_PM10_1H, IAQI_CO_1H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		if(AQI>50){
			if(AQI !=0 && AQI == IAQI_SO2_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("二氧化硫");
				}else{
					sBuffer.append(",二氧化硫");
				}
			}
			if(AQI !=0 && AQI == IAQI_NO2_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("二氧化氮");
				}else{
					sBuffer.append(",二氧化氮");
				}
			}
			if(AQI !=0 && AQI == IAQI_CO_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("一氧化碳");
				}else{
					sBuffer.append(",一氧化碳");
				}
			}
			if(AQI !=0 && AQI == IAQI_O3_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("臭氧1小时");
				}else{
					sBuffer.append(",臭氧1小时");
				}
			}
			if(AQI !=0 && AQI == IAQI_O3_8H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("臭氧8小时");
				}else{
					sBuffer.append(",臭氧8小时");
				}
			}
			if(AQI !=0 && AQI == IAQI_PM10_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("颗粒物(PM10)");
				}else{
					sBuffer.append(",颗粒物(PM10)");
				}
			}
			if(AQI !=0 && AQI == IAQI_PM25_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("颗粒物(PM2.5)");
				}else{
					sBuffer.append(",颗粒物(PM2.5)");
				}
			}
		}
		return sBuffer.toString().equals("") ? "无" : sBuffer.toString();
	
	}
	
	
	//获取首要污染物（24小时）
	public static String getPrimaryPollution_24H(Map<String, Double> map){

		int IAQI_SO2_24H =0;
		int IAQI_NO2_24H =0;
		int IAQI_PM10_1H =0;
		int IAQI_CO_24H =0;
		int IAQI_O3_1H =0;
		int IAQI_PM25_1H =0;
		int IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			IAQI_SO2_24H = GetAQI.getIAQI_24H("SO2",map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_24H = GetAQI.getIAQI_24H("NO2",map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = GetAQI.getIAQI_24H("PM10",map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_24H = GetAQI.getIAQI_24H("CO",map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = GetAQI.getIAQI_24H("O3",map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = GetAQI.getIAQI_24H("PM2.5",map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = GetAQI.getIAQI_24H("O38H", map.get("O38H"));
		}
		StringBuffer sBuffer = new StringBuffer("");
		int AQI = GetAQI.Max(IAQI_SO2_24H, IAQI_NO2_24H, IAQI_PM10_1H, IAQI_CO_24H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		if(AQI>50){
			if(AQI !=0 && AQI == IAQI_SO2_24H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("二氧化硫");
				}else{
					sBuffer.append(",二氧化硫");
				}
			}
			if(AQI !=0 && AQI == IAQI_NO2_24H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("二氧化氮");
				}else{
					sBuffer.append(",二氧化氮");
				}
			}
			if(AQI !=0 && AQI == IAQI_CO_24H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("一氧化碳");
				}else{
					sBuffer.append(",一氧化碳");
				}
			}
			if(AQI !=0 && AQI == IAQI_O3_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("臭氧1小时");
				}else{
					sBuffer.append(",臭氧1小时");
				}
			}
			if(AQI !=0 && AQI == IAQI_O3_8H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("臭氧8小时");
				}else{
					sBuffer.append(",臭氧8小时");
				}
			}
			if(AQI !=0 && AQI == IAQI_PM10_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("颗粒物(PM10)");
				}else{
					sBuffer.append(",颗粒物(PM10)");
				}
			}
			if(AQI !=0 && AQI == IAQI_PM25_1H){
				if(sBuffer.toString().equals("")){
					sBuffer.append("颗粒物(PM2.5)");
				}else{
					sBuffer.append(",颗粒物(PM2.5)");
				}
			}
		}
		return sBuffer.toString().equals("") ? "无" : sBuffer.toString();
	
	}
	
	
	/**
	 * author : Bruce
	 * @param SO2 二氧化硫(SO2)1小时平均浓度值
	 * @param NO2 二氧化氮(NO2)1小时平均浓度值
	 * @param PM10 PM10 1小时平均浓度值
	 * @param CO 一氧化碳(CO) 1小时平均浓度值
	 * @param O3 臭氧(O3) 1小时平均浓度值
	 * @param PM2.5 PM2.5 1小时平均浓度值
	 * @return
	 */
	public static int Max(int SO2, int NO2, int PM10 , int CO, int O3, int PM25,int O38H){
	 	int	max = 0;
	 	if(max <= SO2){
	 		max = SO2;
	 	}
	 	if(max <= NO2){
	 		max = NO2;
	 	}
	 	if(max <= PM10){
	 		max=PM10;
	 	}
	 	if(max <= CO){
	 		max = CO;
	 	}
	 	if(max <= O3){
	 		max =O3;
	 	}
	 	if(max <= PM25){
	 		max=PM25;
	 	}
	 	if(max <= O38H){
	 		max = O38H;
	 	}
	 	return max;
	}
	
	//获取每个因子的IAQI值,并存入Map回调(1小时)
	public static int getIAQI(String name ,double value){
			if(name.toUpperCase().equals("SO2")){
				return GetAQI.getIAQI_SO2_1H(value);
			}else if(name.toUpperCase().equals("NO2")){
				return  GetAQI.getIAQI_NO2_1H(value);
			}else if(name.toUpperCase().equals("PM10")){
				return GetAQI.getIAQI_PM10_24H(value);
			}else if(name.toUpperCase().equals("CO")){
				return GetAQI.getIAQI_CO_1H(value);
			}else if(name.toUpperCase().equals("O3")){
				return GetAQI.getIAQI_O3_1H(value);
			}else if(name.toUpperCase().equals("O38H")){
				return getIAQI_O3_8H(value);
			}else if(name.toUpperCase().equals("PM25") || name.toUpperCase().equals("PM2.5")){
				return   GetAQI.getIAQI_PM25_24H(value);
			}else{
				return 0;
			}
	}
	
	//获取每个因子的IAQI值,并存入Map回调（24小时）
	public static int getIAQI_24H(String name ,double value){
			if(name.toUpperCase().equals("SO2")){
				return GetAQI.getIAQI_SO2_24H(value);
			}else if(name.toUpperCase().equals("NO2")){
				return  GetAQI.getIAQI_NO2_24H(value);
			}else if(name.toUpperCase().equals("PM10")){
				return GetAQI.getIAQI_PM10_24H(value);
			}else if(name.toUpperCase().equals("CO")){
				return GetAQI.getIAQI_CO_24H(value);
			}else if(name.toUpperCase().equals("O3")){
				return GetAQI.getIAQI_O3_1H(value);
			}else if(name.toUpperCase().equals("O38H")){
				return getIAQI_O3_8H(value);
			}else if(name.toUpperCase().equals("PM25") || name.toUpperCase().equals("PM2.5")){
				return   GetAQI.getIAQI_PM25_24H(value);
			}else{
				return 0;
			}
	}
	
	//二氧化硫1小时计算方法
	private static int getIAQI_SO2_1H(double SO2){
		int IAQI = 0;
//		SO2 = Math.round(SO2);
		if (SO2 >= 0 && SO2 <= 150){
            IAQI = Linear(50,0,150,0,SO2);
        }else if (SO2 >= 150 && SO2 <= 500){
            IAQI = Linear(100,50,500,150,SO2);
        }else if (SO2 >= 500 && SO2 <= 650){
            IAQI = Linear(150,100,650,500,SO2);
        }else if (SO2 >= 650 && SO2 <= 800){
            IAQI = Linear(200,150,800,650,SO2);
        }else{
//            IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	//二氧化硫24小时计算方法
	private static int getIAQI_SO2_24H(double SO2){
		int IAQI=0;
		SO2 = Math.round(SO2);
		if (SO2 >= 0 && SO2 <= 50){
	        IAQI = Linear(50,0,50,0,SO2);
	    }else if (SO2 >= 50 && SO2 <= 150){
	        IAQI = Linear(100,50,150,50,SO2);
	    }else if (SO2 >= 150 && SO2 <= 475){
	        IAQI = Linear(150,100,475,150,SO2);
	    }else if (SO2 >= 475 && SO2 <= 800){
	        IAQI = Linear(200,150,800,475,SO2);
	    }else if (SO2 >= 800 && SO2 <= 1600){
	         IAQI = Linear(300,200,1600,800,SO2);
	    }else if (SO2 >= 1600 && SO2 <= 2100){
	        IAQI = Linear(400,300,2100,1600,SO2);
	    }else if (SO2 >= 2100 && SO2 <= 2620){
	        IAQI = Linear(500,400,2620,2100,SO2);
	    }else if(SO2 > 2620){
//	        IAQI = -1;
	    	IAQI = 500;
	    }
		return IAQI;
	}
	
	//no2 1小时计算方法
	private static int getIAQI_NO2_1H(double NO2){
		int IAQI = 0;
//		NO2 = Math.round(NO2);
		if (NO2 >= 0 && NO2 <= 100){
            IAQI = Linear(50,0,100,0,NO2);
        }else if (NO2 >= 100 && NO2 <= 200){
            IAQI = Linear(100,50,200,100,NO2);
        }else if (NO2 >= 200 && NO2 <= 700){
            IAQI = Linear(150,100,700,200,NO2);
        }else if (NO2 >= 700 && NO2 <= 1200){
           IAQI = Linear(200,150,1200,700,NO2);
        }else if (NO2 >= 1200 && NO2 <= 2340){
            IAQI = Linear(300,200,2340,1200,NO2);
        }else if (NO2 >= 2340 && NO2 <= 3090){
            IAQI = Linear(400,300,3090,2340,NO2);
        }else if (NO2 >= 3090 && NO2 <= 3840){
            IAQI = Linear(500,400,3840,3090,NO2);
        }else if(NO2 > 3840){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	//no2 24小时计算方法
	private static int getIAQI_NO2_24H(double NO2){
		int IAQI = 0;
		if (NO2 >= 0 && NO2 <= 40){
            IAQI = Linear(50,0,40,0,NO2);
        }else if (NO2 >= 40 && NO2 <= 80){
            IAQI = Linear(100,50,80,40,NO2);
        }else if (NO2 >= 80 && NO2 <= 180){
            IAQI = Linear(150,100,180,80,NO2);
        }else if (NO2 >= 180 && NO2 <= 280){
           IAQI = Linear(200,150,280,180,NO2);
        }else if (NO2 >= 280 && NO2 <= 565){
            IAQI = Linear(300,200,565,280,NO2);
        }else if (NO2 >= 565 && NO2 <= 750){
            IAQI = Linear(400,300,750,565,NO2);
        }else if (NO2 >= 750 && NO2 <= 940){
            IAQI = Linear(500,400,940,750,NO2);
        }else if(NO2 > 940){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	private static int getIAQI_PM10_24H(double PM10){
		int IAQI =0;
//		PM10 = Math.round(PM10);
		if (PM10 >= 0 && PM10 <= 50){
            IAQI = Linear(50,0,50,0,PM10);
        }else if (PM10 >= 50 && PM10 <= 150){
            IAQI = Linear(100,50,150,50,PM10);
        }else if (PM10 >= 150 && PM10 <= 250){
            IAQI = Linear(150,100,250,150,PM10);
        }else if (PM10 >= 250 && PM10 <= 350){
            IAQI = Linear(200,150,350,250,PM10);
        }else if (PM10 >= 350 && PM10 <= 420){
            IAQI = Linear(300,200,420,350,PM10);
        }else if (PM10 >= 420 && PM10 <= 500){
            IAQI = Linear(400,300,500,420,PM10);
        }else if (PM10 >= 500 && PM10 <= 600){
            IAQI = Linear(500,400,600,500,PM10);
        }else if(PM10 > 600){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	private static int getIAQI_CO_1H(double CO){
		int IAQI =0;
		double C = CO;
		if (C >= 0 && C <= 5){
            IAQI = Linear(50,0,5,0,C);
        }else if (C >= 5 && C <= 10){
            IAQI = Linear(100,50,10,5,C);
        }else if (C >= 10 && C <= 35){
            IAQI = Linear(150,10,35,10,C);
        }else if (C >= 35 && C <= 60){
            IAQI = Linear(200,150,60,35,C);
        }else if (C >= 60 && C <= 90){
            IAQI = Linear(300,200,90,60,C);
        }else if (C >= 90 && C <= 120){
            IAQI = Linear(400,300,120,90,C);
        }else if (C >= 120 && C <= 150){
            IAQI = Linear(500,400,150,120,C);
        }else if(C > 150){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	private static int getIAQI_CO_24H(double CO){
		int IAQI =0;
		double C = CO;
		if (C >= 0 && C <= 2){
            IAQI = Linear(50,0,2,0,C);
        }else if (C >= 2 && C <= 4){
            IAQI = Linear(100,50,4,2,C);
        }else if (C >= 4 && C <= 14){
            IAQI = Linear(150,10,14,4,C);
        }else if (C >= 14 && C <= 24){
            IAQI = Linear(200,150,24,14,C);
        }else if (C >= 24 && C <= 36){
            IAQI = Linear(300,200,36,14,C);
        }else if (C >= 36 && C <= 48){
            IAQI = Linear(400,300,48,36,C);
        }else if (C >= 48 && C <= 60){
            IAQI = Linear(500,400,60,48,C);
        }else if(C > 60){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	private static int getIAQI_O3_1H(double O3){
		int IAQI =0;
//		O3 = Math.round(O3);
		if (O3 >= 0 && O3 <= 160){
            IAQI = Linear(50,0,160,0,O3);
        }else if (O3 >= 160 && O3 <= 200){
            IAQI = Linear(100,50,200,160,O3);
        }else if (O3 >= 200 && O3 <= 300){
            IAQI = Linear(150,100,300,200,O3);
        }else if (O3 >= 300 && O3 <= 400){
            IAQI = Linear(200,150,400,300,O3);
        }else if (O3 >= 400 && O3 <= 800){
            IAQI = Linear(300,200,800,400,O3);
        }else if (O3 >= 800 && O3 <= 1000){
            IAQI = Linear(400,300,1000,800,O3);
        }else if (O3 >= 1000 && O3 <= 1200){
            IAQI = Linear(500,400,1200,1000,O3);
        }else if(O3 > 1200){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
	private static int getIAQI_PM25_24H(double PM25){
		int IAQI =0;
//		PM25 = (Math.round(PM25*10))/10;
		if (PM25 >= 0 && PM25 <= 35){
            IAQI = Linear(50,0,35,0,PM25);
        }else if (PM25 >= 35 && PM25 <= 75){
            IAQI = Linear(100,50,75,35,PM25);
        }else if (PM25 >= 75 && PM25 <= 115){
            IAQI = Linear(150,100,115,75,PM25);
        }else if (PM25 >= 115 && PM25 <= 150){
            IAQI = Linear(200,150,150,115,PM25);
        }else if (PM25 >= 150 && PM25 <= 250){
            IAQI = Linear(300,200,250,150,PM25);
        }else if (PM25 >= 250 && PM25 <= 350){
            IAQI = Linear(400,300,350,250,PM25);
        }else if (PM25 >= 350 && PM25 <= 500){
            IAQI = Linear(500,400,500,350,PM25);
        }else if(PM25 > 500){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	

	private static int getIAQI_O3_8H(double O38H){
		int IAQI = 0;
//		O38H = Math.round(O38H);
		if (O38H >= 0 && O38H <= 100){
            IAQI = Linear(50,0,100,0,O38H);
        }else if (O38H >= 100 && O38H <= 160){
            IAQI = Linear(100,50,160,100,O38H);
        }else if (O38H >= 160 && O38H <= 215){
            IAQI = Linear(150,100,215,160,O38H);
        }else if (O38H >= 215 && O38H <= 265){
            IAQI = Linear(200,150,265,215,O38H);
        }else if (O38H >= 265 && O38H <= 800){
            IAQI = Linear(300,200,800,265,O38H);
        }else if(O38H > 800){
//        	IAQI = -1;
        	IAQI = 500;
        }
		return IAQI;
	}
	
//因子线性化
	private static int Linear(double IAQIhigh, double IAQIlow, double BPhigh, double BPlow, double Concentration){
		double tempIAQI = ((Concentration - BPlow)/(BPhigh-BPlow))*(IAQIhigh-IAQIlow) +IAQIlow;
		BigDecimal bigDecimal = new BigDecimal(tempIAQI);
		int IAQI = (int)Math.ceil(bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()); //计算分指数需向上取整(27.0向上取整为27,27.01向上取整为28)
		return IAQI;
	}
	
	public static String getFactorColor(String factor, Double value){
		String color = "";
		if("PM10".equals(factor)) {
			if(value >= 0 && value <= 50) {
				color = "grade-bgA";
			}else if(value > 50 && value <= 150) {
				color = "grade-bgB";
			}else if(value > 150 && value <= 250) {
				color = "grade-bgC";
			}else if(value > 250 && value <= 350) {
				color = "grade-bgD";
			}else if(value > 350 && value <= 420) {
				color = "grade-bgE";
			}else if(value > 420 && value <= 600) {
				color = "grade-bgF";
			}else if(value > 600) {
				color = "grade-bgF";
			}
		}else if("PM25".equals(factor) || "PM2.5".equals(factor)) {
			if(value >= 0 && value <= 35) {
				color = "grade-bgA";
			}else if(value > 35 && value <= 75) {
				color = "grade-bgB";
			}else if(value > 75 && value <= 115) {
				color = "grade-bgC";
			}else if(value > 115 && value <= 150) {
				color = "grade-bgD";
			}else if(value > 150 && value <= 250) {
				color = "grade-bgE";
			}else if(value > 250 && value <= 500) {
				color = "grade-bgF";
			}else if(value > 500) {
				color = "grade-bgF";
			}
		}else if("SO2".equals(factor)) {
			if(value >= 0 && value <= 150) {
				color = "grade-bgA";
			}else if(value > 150 && value <= 500) {
				color = "grade-bgB";
			}else if(value > 500 && value <= 650) {
				color = "grade-bgC";
			}else if(value > 650 && value <= 800) {
				color = "grade-bgD";
			}else if(value > 800) {
				color = "grade-bgE";
			}
		}else if("NO2".equals(factor)) {
			if(value >= 0 && value <= 100) {
				color = "grade-bgA";
			}else if(value > 100 && value <= 200) {
				color = "grade-bgB";
			}else if(value > 200 && value <= 700) {
				color = "grade-bgC";
			}else if(value > 700 && value <= 1200) {
				color = "grade-bgD";
			}else if(value > 1200 && value <= 2340) {
				color = "grade-bgE";
			}else if(value > 2340 && value <= 3840) {
				color = "grade-bgF";
			}else if(value > 3840) {
				color = "grade-bgF";
			}
		}else if("NO".equals(factor)) {
			if(value >= 0 && value <= 100) {
				color = "grade-bgA";
			}else if(value > 100 && value <= 200) {
				color = "grade-bgB";
			}else if(value > 200 && value <= 700) {
				color = "grade-bgC";
			}else if(value > 700 && value <= 1200) {
				color = "grade-bgD";
			}else if(value > 1200 && value <= 2340) {
				color = "grade-bgE";
			}else if(value > 2340 && value <= 3840) {
				color = "grade-bgF";
			}else if(value > 3840) {
				color = "grade-bgF";
			}
		}else if("NOx".equals(factor)) {
			if(value >= 0 && value <= 100) {
				color = "grade-bgA";
			}else if(value > 100 && value <= 200) {
				color = "grade-bgB";
			}else if(value > 200 && value <= 700) {
				color = "grade-bgC";
			}else if(value > 700 && value <= 1200) {
				color = "grade-bgD";
			}else if(value > 1200 && value <= 2340) {
				color = "grade-bgE";
			}else if(value > 2340 && value <= 3840) {
				color = "grade-bgF";
			}else if(value > 3840) {
				color = "grade-bgF";
			}
		}else if("CO".equals(factor)) {
			if(value >= 0 && value <= 5) {
				color = "grade-bgA";
			}else if(value > 5 && value <= 10) {
				color = "grade-bgB";
			}else if(value > 10 && value <= 35) {
				color = "grade-bgC";
			}else if(value > 35 && value <= 60) {
				color = "grade-bgD";
			}else if(value > 60 && value <= 90) {
				color = "grade-bgE";
			}else if(value > 90 && value <= 150) {
				color = "grade-bgF";
			}else if(value > 150) {
				color = "grade-bgF";
			}
		}else if("O3".equals(factor)) {
			if(value >= 0 && value <= 160) {
				color = "grade-bgA";
			}else if(value > 160 && value <= 200) {
				color = "grade-bgB";
			}else if(value > 200 && value <= 300) {
				color = "grade-bgC";
			}else if(value > 300 && value <= 400) {
				color = "grade-bgD";
			}else if(value > 400 && value <= 800) {
				color = "grade-bgE";
			}else if(value > 800 && value <= 1200) {
				color = "grade-bgF";
			}else if(value > 1200) {
				color = "grade-bgF";
			}
		}else if("AQI".equals(factor)) {
			if(value > 0 && value <= 50) {
				color = "gradeA";
			}else if(value > 50 && value <= 100) {
				color = "gradeB";
			}else if(value > 100 && value <= 150) {
				color = "gradeC";
			}else if(value > 150 && value <= 200) {
				color = "gradeD";
			}else if(value > 200 && value <= 300) {
				color = "gradeE";
			}else if(value > 300 && value <= 500) {
				color = "gradeF";
			}else if(value > 500) {
				color = "gradeF";
			}else{
				color = "gradeG";
			}
		}else if("QAQI".equals(factor)) {
			if(value > 0 && value <= 50) {
				color = "aqi-excellent";
			}else if(value > 50 && value <= 100) {
				color = "aqi-excellentB";
			}else if(value > 100 && value <= 150) {
				color = "aqi-excellentC";
			}else if(value > 150 && value <= 200) {
				color = "aqi-excellentD";
			}else if(value > 200 && value <= 300) {
				color = "aqi-excellentE";
			}else if(value > 300 && value <= 500) {
				color = "aqi-excellentF";
			}else if(value > 500) {
				color = "aqi-excellentF";
			}else{
				color = "aqi-excellentG";
			}
		}
		return color;
	}
	
	//获取常规6因子各自区间浓度对应等级
	public static String getFactorLevel(String factor, Double value){
		String level = "";
		if("PM10".equals(factor)) {
			if(value >= 0 && value <= 50) {
				level = "优";
			}else if(value > 50 && value <= 150) {
				level = "良";
			}else if(value > 150 && value <= 250) {
				level = "轻度污染";
			}else if(value > 250 && value <= 350) {
				level = "中度污染";
			}else if(value > 350 && value <= 420) {
				level = "重度污染";
			}else if(value > 420 && value <= 600) {
				level = "严重污染";
			}else if(value > 600) {
				level = "严重污染";
			}
		}else if("PM25".equals(factor) || "PM2.5".equals(factor)) {
			if(value >= 0 && value <= 35) {
				level = "优";
			}else if(value > 35 && value <= 75) {
				level = "良";
			}else if(value > 75 && value <= 115) {
				level = "轻度污染";
			}else if(value > 115 && value <= 150) {
				level = "中度污染";
			}else if(value > 150 && value <= 250) {
				level = "重度污染";
			}else if(value > 250 && value <= 500) {
				level = "严重污染";
			}else if(value > 500) {
				level = "严重污染";
			}
		}else if("SO2".equals(factor)) {
			if(value >= 0 && value <= 150) {
				level = "优";
			}else if(value > 150 && value <= 500) {
				level = "良";
			}else if(value > 500 && value <= 650) {
				level = "轻度污染";
			}else if(value > 650 && value <= 800) {
				level = "中度污染";
			}else if(value > 800) {
				level = "重度污染";
			}
		}else if("NO2".equals(factor)) {
			if(value >= 0 && value <= 100) {
				level = "优";
			}else if(value > 100 && value <= 200) {
				level = "良";
			}else if(value > 200 && value <= 700) {
				level = "轻度污染";
			}else if(value > 700 && value <= 1200) {
				level = "中度污染";
			}else if(value > 1200 && value <= 2340) {
				level = "重度污染";
			}else if(value > 2340 && value <= 3840) {
				level = "严重污染";
			}else if(value > 3840) {
				level = "严重污染";
			}
		}else if("NO".equals(factor)) {
			if(value >= 0 && value <= 100) {
				level = "优";
			}else if(value > 100 && value <= 200) {
				level = "良";
			}else if(value > 200 && value <= 700) {
				level = "轻度污染";
			}else if(value > 700 && value <= 1200) {
				level = "中度污染";
			}else if(value > 1200 && value <= 2340) {
				level = "重度污染";
			}else if(value > 2340 && value <= 3840) {
				level = "严重污染";
			}else if(value > 3840) {
				level = "严重污染";
			}
		}else if("NOX".equals(factor)) {
			if(value >= 0 && value <= 100) {
				level = "优";
			}else if(value > 100 && value <= 200) {
				level = "良";
			}else if(value > 200 && value <= 700) {
				level = "轻度污染";
			}else if(value > 700 && value <= 1200) {
				level = "中度污染";
			}else if(value > 1200 && value <= 2340) {
				level = "重度污染";
			}else if(value > 2340 && value <= 3840) {
				level = "严重污染";
			}else if(value > 3840) {
				level = "严重污染";
			}
		}else if("CO".equals(factor)) {
			if(value >= 0 && value <= 5) {
				level = "优";
			}else if(value > 5 && value <= 10) {
				level = "良";
			}else if(value > 10 && value <= 35) {
				level = "轻度污染";
			}else if(value > 35 && value <= 60) {
				level = "中度污染";
			}else if(value > 60 && value <= 90) {
				level = "重度污染";
			}else if(value > 90 && value <= 150) {
				level = "严重污染";
			}else if(value > 150) {
				level = "严重污染";
			}
		}else if("O3".equals(factor)) {
			if(value >= 0 && value <= 160) {
				level = "优";
			}else if(value > 160 && value <= 200) {
				level = "良";
			}else if(value > 200 && value <= 300) {
				level = "轻度污染";
			}else if(value > 300 && value <= 400) {
				level = "中度污染";
			}else if(value > 400 && value <= 800) {
				level = "重度污染";
			}else if(value > 800 && value <= 1200) {
				level = "严重污染";
			}else if(value > 1200) {
				level = "严重污染";
			}
		}
		return level;
	}
	
	/**
	 * 求取有序list中百分位数
	 * @param sorted 有序list 升序
	 * @param p 多少百分位 (PM25,PM10,CO) 95百分位 0.95;(SO2,NO2) 98百分位 0.98,(03_8H) 90百分位 0.9
	 * @return
	 */
	public static double getPercentileData(List<Double> sorted, double p) {
		int size = sorted.size();
		DecimalFormat df=new DecimalFormat("#.###");
		if(size>0){
			int n = sorted.size();
//	        double pos = p * (n+1) ;
	        double pos = 1 + (n-1) * p;//序数
	        if (pos < 1) { 
	        	return Double.valueOf(df.format(sorted.get(0)));
	        }
	        if (pos >= n) { 
	        	return Double.valueOf(df.format(sorted.get(n-1)));
	        }
	        double fpos = Math.floor(pos);
	        int intPos = (int) fpos;//对应序数的整数部分
	        double dif = pos - fpos;//对应序数的小数部分
	        double lower = sorted.get(intPos-1);
	        double upper = sorted.get(intPos);
	        double v = lower + dif * (upper - lower);
	        if(v!=0){
	        	v = Double.valueOf(df.format(v));
	        }
	        return v;
		}
		return 0.0;
	}
	
	/** 
     * 将double格式化为指定小数位的String，不足小数位用0补全 
     * 
     * @param v     需要格式化的数字 
     * @param scale 小数点后保留几位 
     * @return 
     */  
    public static String roundByScale(Double v, int scale) {  
    	if (v==null) {
    		return null;
    	}
        if (scale < 0) {  
            throw new IllegalArgumentException(  
                    "The   scale   must   be   a   positive   integer   or   zero");  
        }  
        if(scale == 0){  
            return new DecimalFormat("0").format(v);  
        }  
        String formatStr = "0.";  
        for(int i=0;i<scale;i++){  
            formatStr = formatStr + "0";  
        }  
        DecimalFormat df = new DecimalFormat(formatStr);
//        df.setRoundingMode(RoundingMode.HALF_UP);//四舍五入必须加这段；*********
        
        return df.format(v);  
    }  
	
}
